package com.sitanyun.merchant.guide.frament.view.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.adapter.EmplyeeAdapter;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.EmployeeBean;
import com.sitanyun.merchant.guide.bean.ForgetBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.FormBody;

/**
 * 员工管理列表
 */
public class EmployeeActivity extends BaseActivity {

    @BindView(R.id.tv_Registration_link)
    TextView tvRegistrationLink;
    @BindView(R.id.rv_employ)
    RecyclerView rvEmploy;
    @BindView(R.id.sw_employ)
    SwipeRefreshLayout swEmploy;
    @BindView(R.id.ll_one)
    LinearLayout llOne;
    @BindView(R.id.ll_todaytwo)
    LinearLayout llTodaytwo;
    // 分页
    private int offset = 1;
    private List<EmployeeBean.DataBean.RecordsBean> orderbeanlist;
    private EmplyeeAdapter productAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_employee;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("员工管理");
        //创建布局管理
        orderbeanlist = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(EmployeeActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvEmploy.setLayoutManager(layoutManager);
        productAdapter = new EmplyeeAdapter(EmployeeActivity.this);
        rvEmploy.setAdapter(productAdapter);
        productAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        offset = 1;
        initokhttps();
    }

    //请求数据
    private void initokhttps() {
        OkHttpUtils.get().url(Urls.querylist)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .addParams("current", String.valueOf(offset))
                .addParams("size", "10")
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {
                swEmploy.setRefreshing(false);
            }

            @Override
            public void onResponse(String response, int id) {
                EmployeeBean employeeBean = new Gson().fromJson(response, EmployeeBean.class);
                swEmploy.setRefreshing(false);

                if (employeeBean.getCode() == 0) {

                    if (offset == 1) {
                        if (employeeBean.getData().getRecords().size() == 0) {
                            llOne.setVisibility(View.GONE);
                            llTodaytwo.setVisibility(View.VISIBLE);
                            return;
                        } else {
                            llOne.setVisibility(View.VISIBLE);
                            llTodaytwo.setVisibility(View.GONE);
                        }
                        orderbeanlist.clear();
                        productAdapter.setNewData(employeeBean.getData().getRecords());
                        orderbeanlist.addAll(employeeBean.getData().getRecords());
                        productAdapter.notifyDataSetChanged();
                    } else {
                        //orderbeanlist.addAll(orderBean.getData());
                        //底部没有数据时
                        if (employeeBean.getData().getRecords().isEmpty()) {
                            productAdapter.loadMoreEnd();
                        } else {
                            //滑动加载数据
                            List<EmployeeBean.DataBean.RecordsBean> data1 = employeeBean.getData().getRecords();
                            orderbeanlist.addAll(data1);
                            productAdapter.addData(data1);
                            productAdapter.notifyDataSetChanged();
                            productAdapter.loadMoreComplete();

                        }

                    }
                } else {
                    ToastUtil.showToast(EmployeeActivity.this, employeeBean.getMsg() + "");
                }

            }
        });
    }

    @Override
    public void initView() {
        //刷新
        swEmploy.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                offset = 1;
                initokhttps();
            }
        });
        //加载更多
        productAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                offset++;
                initokhttps();
            }
        });
        productAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_sh:
                        switch (orderbeanlist.get(position).getStatus()) {
                            // 0审核  1在职  2停用 3 驳回
                            case 0:
                                dialog(orderbeanlist.get(position).getStaffName(),
                                        orderbeanlist.get(position).getStaffId(),
                                        orderbeanlist.get(position).getPhone(),
                                        orderbeanlist.get(position).getNodeName());
                                break;
                            case 1:
                                dialogs(orderbeanlist.get(position).getStaffId());
                                break;
                            case 2:
                                FormBody build = new FormBody.Builder()
                                        .build();
                                OkHttpUtils.put().requestBody(build).url(Urls.startqy + "/" + Long.valueOf(orderbeanlist.get(position).getStaffId()))
                                        .addHeader("Content-Type", "application/json")
                                        .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                                        .build().execute(new StringCallbacks() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {

                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                        ForgetBean forgetBean = new Gson().fromJson(response, ForgetBean.class);
                                        if (forgetBean.getCode() == 0) {
                                            offset = 1;
                                            initokhttps();
                                       //     ToastUtil.showToast(EmployeeActivity.this, "启用成功");
                                        }
                                    }
                                });
                                break;
                            case 3:

                                break;
                        }
                        break;
                    case R.id.ll_layout:
                        //   RegistrationPosterActivity
                        startActivity(new Intent(EmployeeActivity.this, RegistrationPosterActivity.class)
                                .putExtra("starffid", orderbeanlist.get(position).getStaffId() + ""));
                        //  ToastUtil.showToast(EmployeeActivity.this,"shenhe");
                        break;
                }
            }
        });
    }

    private void dialogs(String staffId) {
        // ToastUtil.showToast(MainActivity.this,"未开启通知权限");
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(EmployeeActivity.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(EmployeeActivity.this, R.layout.loginoutdialog_item, null);
        dialog.setContentView(view);
        TextView ts_message = view.findViewById(R.id.ts_message);
        TextView define = view.findViewById(R.id.define);
        TextView cancel = view.findViewById(R.id.cancel);
        cancel.setText("取消");
        define.setText("停用");
        ts_message.setText("停用后，该员工不能登录");
        //取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //停用
        define.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                FormBody build = new FormBody.Builder()
                        .build();
                OkHttpUtils.put().requestBody(build).url(Urls.stopped + "/" + Long.valueOf(staffId))
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                        .build().execute(new StringCallbacks() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ForgetBean forgetBean = new Gson().fromJson(response, ForgetBean.class);
                        if (forgetBean.getCode() == 0) {
                            offset = 1;
                            initokhttps();
                          //  ToastUtil.showToast(EmployeeActivity.this, "停用成功");
                        }
                    }
                });
            }
        });


        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //  window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        //设置弹出动画
        //   window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
        /*添加View到当前显示的Activity*/
        //   MerchantApp.getInstance().showView(view);
    }

    // 弹窗
    private void dialog(String staffName, String starffid, String phone, String storeName) {
        // ToastUtil.showToast(MainActivity.this,"未开启通知权限");
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(EmployeeActivity.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(EmployeeActivity.this, R.layout.loginoutdialog_item, null);
        dialog.setContentView(view);
        TextView ts_message = view.findViewById(R.id.ts_message);
        TextView define = view.findViewById(R.id.define);
        TextView cancel = view.findViewById(R.id.cancel);
        cancel.setText("拒绝");
        define.setText("通过");
        ts_message.setText("是否通过"+staffName + " " + phone + "的加入申请？" );
        //拒绝
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                FormBody build = new FormBody.Builder()
                        .build();

                OkHttpUtils.put().requestBody(build).url(Urls.notpass + "/" + Long.valueOf(starffid))
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                        .build().execute(new StringCallbacks() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ForgetBean forgetBean = new Gson().fromJson(response, ForgetBean.class);
                        if (forgetBean.getCode() == 0) {
                            offset = 1;
                            initokhttps();
                           // ToastUtil.showToast(EmployeeActivity.this, "拒绝成功");
                        }
                    }
                });
            }
        });
        //通过
        define.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                FormBody build = new FormBody.Builder()
                        .build();

                OkHttpUtils.put().requestBody(build).url(Urls.pass + "/" + Long.valueOf(starffid))
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                        .build().execute(new StringCallbacks() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ForgetBean forgetBean = new Gson().fromJson(response, ForgetBean.class);
                        if (forgetBean.getCode() == 0) {
                            offset = 1;
                            initokhttps();
                          //  ToastUtil.showToast(EmployeeActivity.this, "通过");
                        }
                    }
                });
            }
        });


        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //  window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        //设置弹出动画
        //   window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
        /*添加View到当前显示的Activity*/
        //   MerchantApp.getInstance().showView(view);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    //注册链接弹窗
    @OnClick(R.id.tv_Registration_link)
    public void onViewClicked() {
        initdialog();
    }

    private void initdialog() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(EmployeeActivity.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(EmployeeActivity.this, R.layout.registration_link_item, null);
        dialog.setContentView(view);
        //店长id获取
        LinearLayout en_store = view.findViewById(R.id.en_store);
        ImageView im_store = view.findViewById(R.id.im_store);
        TextView tv_storename = view.findViewById(R.id.tv_storename);
        TextView tv_store_position = view.findViewById(R.id.tv_store_position);
        ImageView im_store_cb = view.findViewById(R.id.im_store_cb);
        //导购id获取
        LinearLayout en_shops = view.findViewById(R.id.en_shops);
        ImageView im_shops = view.findViewById(R.id.im_shops);
        TextView tv_shopsname = view.findViewById(R.id.tv_shopsname);
        TextView tv_shopsposition = view.findViewById(R.id.tv_shopsposition);
        ImageView im_shops_cb = view.findViewById(R.id.im_shops_cb);

        //点击店长
        en_store.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Glide.with(EmployeeActivity.this).load(R.mipmap.storses).into(im_store);
                Glide.with(EmployeeActivity.this).load(R.mipmap.en_shops).into(im_shops);

                en_store.setBackground(getResources().getDrawable(R.drawable.money_radio));
                en_shops.setBackground(getResources().getDrawable(R.drawable.money_radio_en));
                tv_storename.setTextColor(Color.parseColor("#ffffffff"));
                tv_shopsname.setTextColor(Color.parseColor("#ff333333"));
                tv_store_position.setTextColor(Color.parseColor("#ffffffff"));
                tv_shopsposition.setTextColor(Color.parseColor("#ff333333"));
                im_store_cb.setVisibility(View.VISIBLE);
                im_shops_cb.setVisibility(View.GONE);

            }
        });
        //点击导购
        en_shops.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Glide.with(EmployeeActivity.this).load(R.mipmap.en_store_wcb).into(im_store);
                Glide.with(EmployeeActivity.this).load(R.mipmap.en_shop_cb).into(im_shops);
                en_store.setBackground(getResources().getDrawable(R.drawable.money_radio_en));
                en_shops.setBackground(getResources().getDrawable(R.drawable.money_radio));
                tv_storename.setTextColor(Color.parseColor("#ff333333"));
                tv_shopsname.setTextColor(Color.parseColor("#ffffffff"));
                tv_store_position.setTextColor(Color.parseColor("#ff333333"));
                tv_shopsposition.setTextColor(Color.parseColor("#ffffffff"));

                im_store_cb.setVisibility(View.GONE);
                im_shops_cb.setVisibility(View.VISIBLE);
            }
        });

        //取消
        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //生成链接
        view.findViewById(R.id.define).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (im_store_cb.getVisibility() == v.getVisibility()) {
                    //1店长，2导购
                    // ToastUtil.showToast(EmployeeActivity.this,"店长");
                    startActivity(new Intent(EmployeeActivity.this, RegiPosterActivity.class).putExtra("zy", "1"));
                } else {
                    startActivity(new Intent(EmployeeActivity.this, RegiPosterActivity.class).putExtra("zy", "2"));

                }
            }
        });


        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //  window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        //设置弹出动画
        //   window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
        /*添加View到当前显示的Activity*/
        //   MerchantApp.getInstance().showView(view);

    }
}
